//
//  HLBoutiqueViewModel.swift
//  HLXiMaLaYa
//
//  Created by hulinhu on 2018/2/7.
//  Copyright © 2018年 hulinhu. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
import RxDataSources
import ObjectMapper

class HLBoutiqueViewModel: NSObject {
    
    private let vmDatas = Variable<[([HLKeywardsModel]?, [HLFocusModel]?, [HLSquareModel]?, [HLBoutiqueIndexModel]?, HLCategoryModel?, [HLRecommendModel])]>([])
    
    private var page: Int = 1
    
    // 网络请求类
    let provider = HLNetWorkProvider<HLRecommendNetManager>()

}

extension HLBoutiqueViewModel: HLViewModelType {
    
     typealias Input = HLBoutiqueInput
     typealias Output = HLBoutiqueOutput
    
     struct HLBoutiqueInput {
        
     }
    
    struct HLBoutiqueOutput : OutputRefreshProtocol {
        
        var refreshStatus: Variable<HLRefreshStatus>
        let sections: Driver<[HLBoutiqueSection]>
        let requestCommand = PublishSubject<Bool>()
        
        init(sections:Driver<[HLBoutiqueSection]>) {
            self.sections = sections
            refreshStatus = Variable<HLRefreshStatus>(.none)
        }
     }
    
    // MARK: - 方法请求
    func transform(input: HLBoutiqueViewModel.HLBoutiqueInput) -> HLBoutiqueViewModel.HLBoutiqueOutput {
        
        let temp_sections = vmDatas.asObservable().map({ (sections) -> [HLBoutiqueSection] in
            return sections.map({ (keywordList, focusList, squareList, indexList, category, models) -> HLBoutiqueSection in
                return HLBoutiqueSection(keywordList: keywordList, focusList: focusList, squareList: squareList, indexList: indexList, category: category, items: models)
            })
        }).asDriver(onErrorJustReturn: [])
        
        let output = HLBoutiqueOutput(sections: temp_sections)
        
        // 网络请求
        output.requestCommand.subscribe(onNext: { [weak self] (isPull) in
            guard let `self` = self else { return }
            if isPull {
                self.provider.request(.GETBoutiquePullList).filterSuccessfulStatusCodes().mapJSON().mapObject(type: HLBoutiqueModel.self).subscribe(onNext: { [weak self](datas) in
                    guard let `self` = self else { return }

                    guard let keywordList = datas.keywordList else { return }
                    guard let focusList = datas.focusList else { return }
                    guard let squareList = datas.squareList else { return }
                    guard let categoryList = datas.categoryList else { return }
                    
                    var temp_category: [HLCategoryModel] = []
                    var temp_categorySubList: [[HLRecommendModel]] = []
                    var temp_indexList: [HLBoutiqueIndexModel] = []
                    
                    // 组装数据
                    for (index, category) in categoryList.enumerated() {
                        
                        guard index >= 5 else { // 过滤
                            continue
                        }
                        
                        guard let categorySubList = category.list, categorySubList.count > 0 else {
                            continue
                        }
                        // 处理数据
                        temp_category.append(category)
                        temp_categorySubList.append(categorySubList)
                        
                        // 存放索引 从【每日优选】开始
                        if index >= 6 {
                            var indexModel = HLBoutiqueIndexModel()
                            indexModel.title = category.title
                            indexModel.index = index - 5
                            temp_indexList.append(indexModel)
                        }
                    }
                    var sectionArr:[([HLKeywardsModel]?, [HLFocusModel]?, [HLSquareModel]?, [HLBoutiqueIndexModel]?, HLCategoryModel?, [HLRecommendModel])] = []
                    
                    for (index, _) in temp_category.enumerated() {
                        sectionArr.append((keywordList, focusList, squareList, temp_indexList, temp_category[index], temp_categorySubList[index]))
                    }
                    // 更新数据
                    self.vmDatas.value = sectionArr
                    
                }).disposed(by: self.rx.disposeBag)
            }
            
            output.refreshStatus.value = .endHeaderRefresh
            output.refreshStatus.value = .endFooterRefresh
            
        }).disposed(by: rx.disposeBag)

        return output
    }
}


struct HLBoutiqueSection {
    
    var keywordList: [HLKeywardsModel]?
    var focusList: [HLFocusModel]?
    var squareList: [HLSquareModel]?
    var indexList: [HLBoutiqueIndexModel]?
    var category: HLCategoryModel?
    var items: [Item]
}

extension HLBoutiqueSection: SectionModelType {
    
    typealias Item = HLRecommendModel
    
    init(original: HLBoutiqueSection, items: [HLRecommendModel]) {
        self = original
        self.items = items
    }
}







